package chapter15;

import java.util.concurrent.TimeUnit;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/23 11:53
 */
public class Racer extends Thread {
    FireFlag fireFlag;

    public Racer(FireFlag fireFlag) {
        this.fireFlag = fireFlag;
    }

    public static void main(String[] args) throws InterruptedException {
        int num = 10;
        FireFlag fireFlag = new FireFlag();
        Thread[] racers = new Thread[num];
        for (int i = 0; i < num; i++) {
            racers[i] = new Racer(fireFlag);
            racers[i].start();
        }

        TimeUnit.SECONDS.sleep(3);
        fireFlag.fire();
    }

    @Override
    public void run() {
        try {
            this.fireFlag.wairForFire();
            System.out.println("start run " + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            System.out.println("exit via interrupt");
        }
    }
}

